package com.lzjy.tw.composedemo.ui.screen.home.note

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.GridCells
import androidx.compose.foundation.lazy.LazyVerticalGrid
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.lzjy.tw.composedemo.domain.model.TestModel
import timber.log.Timber

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Note(viewModel: NoteViewModel, onSelect: (Int) -> Unit) {

    val testList: List<TestModel> by viewModel.testValue.collectAsState()

    if (testList.isEmpty()) {
        Text(" 数据库无数据 ")
    } else {

        LazyVerticalGrid(
            cells = GridCells.Fixed(3)
        ) {
            items(testList.size) {
                NoteItem(
                    testList[it]
                ) {
                    Timber.e("items $it")
                    onSelect(it)
                }
            }
        }
    }

}

@Composable
private fun NoteItem(model: TestModel, onItemClick: () -> Unit) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .height(160.dp)
            .padding(18.dp)
            .clickable(onClick = onItemClick),
        elevation = 8.dp
    ) {

        Text(
            text = "${model.ownerId}",
            textAlign = TextAlign.Center
        )
    }
}
